package base
{
	
	import com.dusk.util.ResUtil;
	import com.game.data.vo.LevelConfigVO;
	import com.game.engine.MainGame;
	import com.game.view.component.MonsterAppearPoint;
	import com.game.view.component.StopPoint;
	import com.game.view.component.TransDoor;
	
	import config.Config;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	
	public class BaseGameScene extends MovieClip
	{
		
		public function BaseGameScene()
		{
			super();
			bgSprite = ResUtil.getNewObj("bg" + MainGame.getIns().levelData.getLevelIDArray()[0] + MainGame.getIns().levelData.getLevelIDArray()[1]);
			bgSprite.x = -20;
			bgSprite.cacheAsBitmap = true;
			bgContainer = getChildByName("bgContainer") as MovieClip;
			setChildIndex(bgContainer, 0);
			bgContainer.addChildAt(bgSprite, 0);
			for (var i:int = 0; i < numChildren; i++)
			{
				Config.getIns().pWorld.addSubObj(getChildAt(i));
			}
		}
		
		public var bgContainer:MovieClip;
		protected var gc:Config;
		private var bgBitmapDataSprite:Sprite;
		private var bgSprite:Sprite;
		private var bgBitmapData:BitmapData;
		
		public function getChildByNameInBgSprite(param1:String):DisplayObject
		{
			var _loc3_:DisplayObject = null;
			var _loc2_:uint = 0;
			while (_loc2_ < bgSprite.numChildren)
			{
				_loc3_ = bgSprite.getChildByName(param1);
				if (_loc3_)
				{
					return _loc3_;
				}
				_loc2_++;
			}
			return null;
		}
		
		public function buildScene(data:LevelConfigVO):void
		{
			var item:Object;
			var singleton:*;
			for each (item in data.stopPointArr)
			{
				singleton = new StopPoint();
				with (StopPoint(singleton))
				{
					if (item.x != undefined && item.y != undefined)
					{
						setXY(item.x, item.y);
						x = item.x;
						y = item.y;
					}
					if (item.idx != undefined)
					{
						idx = item.idx;
					}
				}
				addChild(singleton);
				Config.getIns().pWorld.getStopPointArray().push(singleton);
			}
			for each (item in data.monsterAppearPointArr)
			{
				singleton = new MonsterAppearPoint();
				for (var str:String in item)
				{
					if (str != "" && singleton.hasOwnProperty(str))
					{
						singleton[str] = item[str];
					}
				}
				addChild(singleton);
				Config.getIns().pWorld.getMonsterAppearArray().push(singleton);
			}
			for each (item in data.transDoorArr)
			{
				singleton = new TransDoor();
				for (var str2:String in item)
				{
					if (str2 != "" && singleton.hasOwnProperty(str2))
					{
						singleton[str2] = item[str2];
					}
				}
				addChild(singleton);
				Config.getIns().pWorld.getTransferDoorArray().push(singleton);
			}
		}
		
		public function updateBgSprite():void
		{
			var _loc1_:* = null;
			var _loc2_:int = 0;
			while (_loc2_ < bgSprite.numChildren)
			{
				_loc1_ = bgSprite.getChildAt(0) as Bitmap;
				_loc1_.bitmapData.dispose();
				bgSprite.removeChild(_loc1_);
				_loc2_++;
			}
			var _loc3_:BitmapData = new BitmapData(940, 590, true, 0);
			_loc3_.copyPixels(bgBitmapData, new Rectangle(Math.abs(x), 0, 940, 590), new Point(0, 0));
			bgSprite.addChild(new Bitmap(_loc3_));
			bgSprite.x = -x;
		}
		
		public function destroy():void
		{
			if (bgBitmapData)
			{
				bgBitmapData.dispose();
				bgBitmapData = null;
			}
			bgBitmapDataSprite = null;
			bgSprite = null;
			var _loc1_:int = 0;
			while (_loc1_ < bgContainer.numChildren)
			{
				bgContainer.removeChild(bgContainer.getChildAt(0));
				_loc1_++;
			}
			if (parent)
			{
				parent.removeChild(this);
			}
		}
	}
}
